<?php
/**
 * @ClassName: Payorder
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-12 15:26
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\model;

class Payorder extends Api
{
    /**
     * 创建订单
     * @return \think\response\Json
     */
    public function createOrder()
    {
        $goods_id = input('goods_id', 0);
        $goods_price = input('goods_price', 1);
        $goods_num = input('goods_num', 1);
        $user_note = input('user_note', '');
        $order_type = input('order_type', '1');
        $store_id = input('store_id', 0);
        $marketing_id = input('marketing_id', 0); // 活动id
        $gift_amount = input('gift_amount', 0); // 赠送金额
        $pay_code = input('pay_code', 'wxpay'); // 支付方式

        $order_sn = 'pay_' . date('YmdHis') . mt_rand(100, 999);
        $total_amount = $order_amount = bcmul($goods_price, $goods_num, 2);
        switch ($order_type) {
            case '1':
                // 商户入驻保证金订单
                $shop_apply = model\Merchant::where('id', $goods_id)->find();
                if ($shop_apply) {
                    $store_id = $shop_apply['store_id'];
                }
                // 存在已创建未支付订单
                $order = model\PayOrder::where(['goods_id' => $goods_id, 'order_type' => 1, 'pay_status' => 0])->find();
                if ($order) {
                    $result = [
                        'order_id' => $order['id'],
                        'trade_no' => $order['order_sn'],
                    ];
                    return json(['code' => 1, 'msg' => '订单创建成功', 'data' => $result]);
                }
                break;
            case '2':
                // 代理月结保证金订单
                $agent = model\Agent::where('member_id', $this->member_id)->find();
                $agent_id = 0;
                if ($agent) {
                    $agent_id = $agent['id'];
                }
                $order_data = [
                    'order_sn' => $order_sn,
                    'member_id' => $this->member_id,
                    'agent_id' => $agent_id,
                    'amount' => $goods_price + $gift_amount,//充值金额=支付金额+赠送金额
                    'pay_code' => $pay_code,
                ];
                model\MonthSettleRechargeRecords::create($order_data);
                break;
            case '3':
                // 开户活动付款订单
                $marketing = model\Marketing::where('id', $marketing_id)->find();
                if (!$marketing) {
                    return json(['code' => 0, 'msg' => '活动未找到', 'data' => null]);
                }
                $marketing_id = $marketing['id'];
                $store_id = $marketing['store_id'];
                $total_amount = $order_amount = bcmul($marketing['prices'], $marketing['num'], 2);

                //更新活动信息
                model\Marketing::where('id', $marketing_id)->update([
                    'total_amount' => $total_amount,
                    'surplus_amount' => $total_amount,
                    'update_time' => date('Y-m-d H:i:s'),
                ]);

                // 存在已创建未支付订单
                $order = model\PayOrder::where(['marketing_id' => $marketing_id, 'order_type' => 3, 'pay_status' => 0])->find();
                if ($order) {
                    $result = [
                        'order_id' => $order['id'],
                        'trade_no' => $order['order_sn'],
                    ];
                    return json(['code' => 1, 'msg' => '订单创建成功', 'data' => $result]);
                }
                break;
            case '8':
                //
                break;
            default:
                //
                break;
        }

        $data = [
            'order_sn' => $order_sn,
            'member_id' => $this->member_id,
            'goods_id' => $goods_id,
            'goods_price' => $goods_price,
            'goods_num' => $goods_num,
            'use_money' => 0,
            'use_points' => 0,
            'use_dot' => 0,
            'use_commission' => 0,
            'points_money' => 0,
            'total_amount' => $total_amount,
            'order_amount' => $order_amount,
            'order_prom_id' => 0,
            'order_prom_amount' => 0,
            'discount' => 0,
            'gift_amount' => $gift_amount, // 赠送金额
            'marketing_id' => $marketing_id, // 活动id
            'user_note' => $user_note,
            'admin_note' => '',
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
            'store_id' => $store_id,
            'order_type' => $order_type,
            'pay_code' => $pay_code,
        ];

        $order_id = model\PayOrder::insertGetId($data);
        if ($order_id) {
            $result = [
                'order_id' => $order_id,
                'trade_no' => $order_sn,
            ];
            return json(['code' => 1, 'msg' => '订单创建成功', 'data' => $result]);
        } else {
            return json(['code' => 0, 'msg' => '订单创建失败']);
        }
    }

}